﻿/*
 * Vector for ActionScript 3  
 * Author: Mohammad Haseeb aka M.H.A.Q.S.
 * Original library for Processing by Daniel Shiffman
 * 
 * Licence Agreement
 * 
 * This source code is copyright of Daniel Shiffman, I converted it 
 * to AS3 for my personal convenience.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package Util
{
	import flash.display.*;

	public class Particle extends Shape
	{
		var loc:Vec;
		var vel:Vec;
		var acc:Vec;
		var r:Number;
		var timer:Number;
		var colorMe:uint;
		// One Constructoror
		public function Particle1(a:Vec,v:Vec,l:Vec,r1:Number,colo:uint)
		{
			acc=a.Constructor();
			vel=v.Constructor();
			loc=l.Constructor();
			colorMe = colo;
			r=r1;
			timer=100.0;
		}
		// Another Constructoror (the one we are using here)
		public function Particle(l:Vec,colo:uint)
		{
			acc=new Vec(0.0,0.05,0.0);
			colorMe = colo;
			vel=new Vec(Math.random() * 2.5,Math.random() * 3 - 0.5,0);
			loc=l.Constructor();
			r=20.0;
			timer=1.0;
		}
	
		public function run():void
		{
			update();
			render();
		}

		// Method to update location
		public function update():void
		{
			vel.VectorAddition1(acc);
			loc.VectorAddition1(vel);
			timer-= 0.025;
			acc.setXY(0,0);
		}
		// Method to apply a force vector to the Particle object
		// Note we are ignoring "mass" here
		public function add_force(f:Vec):void
		{
			acc.VectorAddition1(f);
		}

		// Method to display
		public function render():void
		{
			graphics.clear();
			graphics.beginFill(colorMe,timer);
			graphics.lineStyle(0,colorMe,timer);
			graphics.drawEllipse(loc.x,loc.y,2*r,2*r);
			graphics.endFill();
		}
		// Is the particle still useful?
		public function dead():Boolean
		{
			if (timer <= 0.0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}